package com.tan.dom4jdome;

import com.tan.dom4japi.Dom4j;
import com.tan.entity.InfoEntity;
import com.tan.utils.DateUtils;
import com.tan.utils.LogUtils;
import org.apache.jasper.tagplugins.jstl.core.Url;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Dom4jImpl implements Dom4j {
    //存储的集合
    private List<InfoEntity> entities = new ArrayList<InfoEntity>();
    // 存储信息的实体类
    private InfoEntity entity;

    /**
     * 条件查询航班信息
     *
     * @param departureAirportName 起始站
     * @param arrivalAirportName   到达站,
     * @param date  出发的时间
     * @return java.util.List<InfoEntity>
     */
    @Override
    public List<InfoEntity> selectInfo(String departureAirportName, String arrivalAirportName, Date date,String xmlPath) {
        List<InfoEntity> entities = new ArrayList<InfoEntity>();
        List<InfoEntity> infoEntities = null;
        try {
            infoEntities = getInfoEntitys(xmlPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (InfoEntity entity :
                infoEntities) {
            if (entity.getDepartureAirportName().equals(departureAirportName)
                    && entity.getArrivalAirportName().equals(arrivalAirportName)
                    && entity.getArrivalDate().equals(date))
                entities.add(entity);
        }
        try {
            // 开启写如日志
            LogUtils.LogInfo(Dom4jImpl.class, "有人查看了起始站为:" + departureAirportName +
                    " 到达站为：" + arrivalAirportName + "的航班信息");
        } catch (Exception e) {
            System.out.println("路径错误");

        }

        return entities;
    }

    /**
     * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
     * @Date 2018/8/16 10:34
     * @Description 获取航班的信息存入数组
     */
    @Override
    public List<InfoEntity> getInfoEntitys(String xmlPath) throws Exception {
        SAXReader saxReader = new SAXReader();
        try {
            URL url = this.getClass().getClassLoader().getResource(xmlPath);//user.dir指定了当前的路径
            Document document = saxReader.read(url);
            Element root = document.getRootElement();
            Element ele = root.element("Body");
            parserNode(ele, "getInfoEntitys");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LogUtils.LogInfo(Dom4jImpl.class, "获取节点异常");
            throw new Exception("获取节点异常");
        }
        Collections.sort(entities);
        return entities;

    }

    /**
     * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
     * @Date 2018/8/18 00:49
     * @Description 第二种获取数组的方法
     */
    @Override
    public List<InfoEntity> getInfoEntitys2(String xmlPath) {
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(new File(xmlPath));
            //获取Segment下面所有的子节点
            List<Element> elements = document.selectNodes("//Segment");
            //Element elements = (Element)document.selectSingleNode("//Flight/Segment");
            //Dom4j.parserNode(elements,"getInfoEntity");
            for (Element element : elements) {
                if (element.getName().equals("Departure")
                        || element.getName().equals("Arrival")
                        || element.getName().equals("Carrier")
                        || element.getName().equals("ElectronicTicketing")
                        || element.getName().equals("Classes")) {
                    parserNode(element, "getInfoEntitys2");
                }
            }
        } catch (Exception e) {
            LogUtils.LogInfo(Dom4jImpl.class, "获取节点异常");
        }
        System.out.println(getEntities());
        return getEntities();
    }

    /**
     * @param ele        传入的哪个节点的信息
     * @param methodName 掉用方法的名称
     * @return void
     */
    @Override
    public void parserNode(Element ele, String methodName) {
        String info = "";
        String status = "";
        List<Element> eleList = ele.elements();
        for (Element e1 : eleList) {
            //从Users根节点开始遍历，像【属性=值】的形式存为一个Attribute对象存储在List集合中·
            if (ele.getName().equals("Departure")) {
                if (e1.getName().equals("AirportCode")) {
                    entity = new InfoEntity();
                    entity.setDepartureAirportCode(e1.getText().trim());
                }
                if (e1.getName().equals("AirportName")) {
                    entity.setDepartureAirportName(e1.getText().trim());
                }
                if (e1.getName().equals("Date")) {
                    entity.setDepartureDate(DateUtils.formatDate(e1.getText().trim()));
                }
                if (e1.getName().equals("Time")) {
                    entity.setDepartureTime(DateUtils.formatTime(e1.getText().trim()));
                }
                if (e1.getName().equals("Terminal")) {
                    entity.setDepartureTerminal(e1.getText().trim());
                }
            }
            if (ele.getName().equals("Arrival")) {
                if (e1.getName().equals("AirportCode")) {
                    entity.setArrivalAirportCode(e1.getText().trim());
                }
                if (e1.getName().equals("AirportName")) {
                    entity.setArrivalAirportName(e1.getText().trim());
                }
                if (e1.getName().equals("Date")) {
                    entity.setArrivalDate(DateUtils.formatDate(e1.getText().trim()));
                }
                if (e1.getName().equals("Time")) {
                    entity.setArrivalTime(DateUtils.formatTime(e1.getText().trim()));
                }
                if (e1.getName().equals("Terminal")) {
                    entity.setArrivalTerminal(e1.getText().trim());
                }
            }
            if (ele.getName().equals("Carrier")) {
                if (e1.getName().equals("FlightNumber")) {
                    entity.setFlightNumber(Integer.parseInt(e1.getText().trim()));
                }
                if(e1.getName().equals("AirlineCode")){
                    entity.setAirlineCode(e1.getText().trim());

                }
            }

            if (ele.getName().equals("Classes")) {
                //遍历子节点的信息并存储
                for (Element e2 : eleList) {
                    info = info + e2.getText().trim() + ",";
                }
                for (Element e2 : eleList) {
                    Attribute attr = e2.attribute("Status");
                    if(attr !=null){
                    status = status +attr.getValue() + ",";}
                }
                entity.setStatus(status);
                entity.setClasses(info);
                info = "";
                status = "";
            }
        }
        if (ele.getName().equals("ElectronicTicketing")) {
            entity.setElectronicTicketing(ele.getText().trim());
            entities.add(entity);
        }
        //递归遍历父节点下的所有子节点
        if (methodName.equals("getInfoEntitys")) {
            for (Element e2 : eleList) {
                parserNode(e2, methodName);
            }
        }
    }

    public List<InfoEntity> getEntities() {
        return this.entities;
    }


}
